﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Reflection;
using System.Text.RegularExpressions;
using Vbyte.SharpOrm.Config;
using System.Data;

namespace Vbyte.SharpOrm
{
    public static partial class OrmHelper
    {
        #region Insert

        /// <summary>
        /// 添加到相关数据库
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry)
        {
            return Insert(SqlTableEntry, null);
        }

        /// <summary>
        /// 添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="getIdentity">是否获取最新生成的标志</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry, bool getIdentity)
        {
            return Insert(SqlTableEntry, null, getIdentity);
        }

        /// <summary>
        /// 使用已打开的共享连接，添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="SharedOpenConnection">已打开的共享数据库连接，只使用不关闭及销毁。</param>
        /// <param name="getIdentity">是否获取最新生成的标志</param>
        /// <param name="NotIgnoreDefaultValueProperties">强制不忽略默认值的字段集合</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry, DbConnection SharedOpenConnection, bool getIdentity, string[] NotIgnoreDefaultValueProperties)
        {
            return Insert(SqlTableEntry, SharedOpenConnection, getIdentity, NotIgnoreDefaultValueProperties, null);
        }

        /// <summary>
        /// (事务支持)使用已打开的共享连接，添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="SharedOpenConnection">已打开的共享数据库连接，只使用不关闭及销毁。</param>
        /// <param name="getIdentity">是否获取最新生成的标志</param>
        /// <param name="NotIgnoreDefaultValueProperties">强制不忽略默认值的字段集合</param>
        /// <param name="trans">当前关联的数据库事务</param>
        /// <returns>如果获取最后新增的标志则返回标志值，否则为null。</returns>
        public static object Insert(this TableEntry SqlTableEntry, DbConnection SharedOpenConnection, bool getIdentity, string[] NotIgnoreDefaultValueProperties, DbTransaction trans)
        {
            object result = null;
            string tabName, sqlIdentity = null;
            AdoNetAdapter ormAdp = GetOrmAdapter(SqlTableEntry, out tabName);
            if (getIdentity == true) sqlIdentity = ormAdp.GetSqlDbAdapter().GetIdentitySql(tabName);

            DbCommand cmd = GeneratorDbCommand(SqlTableEntry, SQLRowCommandType.Insert, NotIgnoreDefaultValueProperties);
            if (trans != null) cmd.Transaction = trans;
            if (SharedOpenConnection != null)
            {
                cmd.Connection = SharedOpenConnection;
                if (SharedOpenConnection.State == ConnectionState.Closed)
                {
                    SharedOpenConnection.Open();
                }
                result = cmd.ExecuteNonQuery();
                if (getIdentity)
                {
                    cmd.CommandText = sqlIdentity;
                    result = cmd.ExecuteScalar();
                }
            }
            else
            {
                DbConnection conn = ormAdp.GetDbConnection();
                conn.Open();

                cmd.Connection = conn;
                result = cmd.ExecuteNonQuery();

                if (getIdentity)
                {
                    cmd.Parameters.Clear();
                    cmd.CommandText = sqlIdentity;
                    result = cmd.ExecuteScalar();
                }

                conn.Close();
                conn.Dispose();
            }
            cmd.Dispose();
            ormAdp.Dispose();
            return result;
        }

        /// <summary>
        /// 添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="getIdentity">是否获取最新生成的标志</param>
        /// <param name="NotIgnoreDefaultValueProperties">强制不忽略默认值的字段集合</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry, bool getIdentity, string[] NotIgnoreDefaultValueProperties)
        {
            return Insert(SqlTableEntry, null, getIdentity, NotIgnoreDefaultValueProperties);
        }

        /// <summary>
        /// 使用已打开的共享连接，添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="SharedOpenConnection">已打开的共享数据库连接，只使用不关闭及销毁。</param>
        /// <param name="getIdentity">是否获取最新生成的标志</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry, DbConnection SharedOpenConnection, bool getIdentity)
        {
            return Insert(SqlTableEntry, SharedOpenConnection, getIdentity, new string[0]);
        }

        /// <summary>
        /// 使用已打开的共享连接，添加到相关数据库。
        /// </summary>
        /// <param name="SqlTableEntry">表映射实例</param>
        /// <param name="SharedOpenConnection">已打开的共享数据库连接，只使用不关闭及销毁。</param>
        /// <returns></returns>
        public static object Insert(this TableEntry SqlTableEntry, DbConnection SharedOpenConnection)
        {
            return Insert(SqlTableEntry, SharedOpenConnection, false);
        }

        #endregion
	}
}
